package com.solo.filterdemo01;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
import java.io.PrintWriter;

// @WebFilter(filterName = "Filter1", urlPatterns = "/Servlet01")
public class Filter1 implements Filter {
    public void init(FilterConfig filterConfig) {
    }

    public void destroy() {
    }

    public void doFilter(ServletRequest request,
                         ServletResponse response,
                         FilterChain chain)
            throws ServletException, IOException {
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();

        out.println("<b> Filter1过滤请求 : </b>");
        out.println("Hello " + request.getParameter("name") + "!");

        out.println("<br/>");

        // 执行doFilter()调用链中的下一个过滤器并执行。
        // 如果没有过滤器则执行所请求的web资源。
        chain.doFilter(request, response);
        out.println("<br/>");
        // 处理请求，调用请求的web资源
        out.println("<b> Filter1过滤响应: </b>");
        out.println("Bye " + request.getParameter("name") + "!");
    }

}
